package in.iqing.view.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.widget.SeekBar;

import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;

import org.apache.commons.io.FileUtils;

import in.iqing.app.R;
import in.iqing.base.BasePrefrenceFragment;
import in.iqing.control.util.ToastUtil;
import in.iqing.model.sp.SettingSP;
import in.iqing.module.data.DataHelper;
import in.iqing.module.data.task.ClearCacheTask;
import in.iqing.view.dialog.ClearCacheDialog;

/**
 * Created by neavo on 2014/6/28.
 */

public class SettingFragment extends BasePrefrenceFragment {

	private Preference size;
	private Preference space;
	private Preference update;
	private Preference clearcache;

	private CheckBoxPreference nightmode;
	private CheckBoxPreference alwayson;
	private CheckBoxPreference traditional;

	private AlertDialog dialog;
	private ClearCacheDialog clearCacheDialog;

	public SettingFragment() {
	}

	public static synchronized SettingFragment newInstance() {
		return new SettingFragment();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.setting_preference);
	}

	@Override
	public void onPause() {
		super.onPause();

		if (dialog != null) dialog.dismiss();
		if (clearCacheDialog != null) clearCacheDialog.dismiss();
	}

	@Override
	protected void onInitialize(Bundle savedInstanceState) {
		super.onInitialize(savedInstanceState);

		initVariables();
		initPreference();
		initEventListener();
	}

	private void initVariables() {
		this.clearCacheDialog = new ClearCacheDialog(getActivity());
	}

	private void initPreference() {
		this.size = findPreference("size");
		this.space = findPreference("space");
		this.update = findPreference("update");
		this.clearcache = findPreference("clearcache");
		this.nightmode = (CheckBoxPreference) findPreference("nightmode");
		this.alwayson = (CheckBoxPreference) findPreference("alwayson");
		this.traditional = (CheckBoxPreference) findPreference("traditional");

		size.setSummary(getString(
				R.string.activity_setting_basic_size_summary,
				SettingSP.getSingleton(getContext()).getSize()
		));

		space.setSummary(getString(
				R.string.activity_setting_basic_space_summary,
				SettingSP.getSingleton(getContext()).getSpace()
		));

		nightmode.setChecked(SettingSP.getSingleton(getContext()).getNightMode());
		alwayson.setChecked(SettingSP.getSingleton(getContext()).getAlwaysOn());
		traditional.setChecked(SettingSP.getSingleton(getContext()).getTraditional());
	}

	private void initEventListener() {
		size.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				return onSizePreferenceClick(preference);
			}
		});

		space.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				return onSpacePreferenceClick(preference);
			}
		});

		update.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				return onUpdatePreferenceClick(preference);
			}
		});

		clearcache.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				return onClearCachePreferenceClick(preference);
			}
		});

		nightmode.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

			@Override
			public boolean onPreferenceChange(Preference preference, Object value) {
				return onNightModePreferenceChange(preference, value);
			}
		});

		alwayson.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

			@Override
			public boolean onPreferenceChange(Preference preference, Object value) {
				return onAlwaysOnPreferenceChange(preference, value);
			}
		});

		traditional.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

			@Override
			public boolean onPreferenceChange(Preference preference, Object value) {
				return onTradionalPreferenceChange(preference, value);
			}
		});
	}

	public boolean onSizePreferenceClick(Preference preference) {
		SeekBar seek = new SeekBar(getContext());
		dialog = new AlertDialog.Builder(getActivity()).create();

		seek.setMax(42);
		seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seek, int progress, boolean fromUser) {
				size.setSummary(getString(
						R.string.activity_setting_basic_size_summary,
						progress + 6
				));

				dialog.setTitle(size.getSummary());
				SettingSP.getSingleton(getContext()).setSize(progress + 6);
			}

			@Override
			public void onStartTrackingTouch(SeekBar seek) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seek) {
			}
		});
		seek.setProgress(SettingSP.getSingleton(getContext()).getSize() - 6);

		dialog.setTitle(size.getSummary());
		dialog.setView(seek);
		dialog.show();

		return true;
	}

	public boolean onSpacePreferenceClick(Preference preference) {
		SeekBar seek = new SeekBar(getContext());
		dialog = new AlertDialog.Builder(getActivity()).create();

		seek.setMax(24);
		seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				space.setSummary(getString(
						R.string.activity_setting_basic_space_summary,
						progress
				));

				dialog.setTitle(space.getSummary());
				SettingSP.getSingleton(getContext()).setSpace(progress);
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
		seek.setProgress(SettingSP.getSingleton(getContext()).getSpace());

		dialog.setTitle(space.getSummary());
		dialog.setView(seek);
		dialog.show();

		return true;
	}

	public boolean onUpdatePreferenceClick(Preference preference) {
		UmengUpdateAgent.setUpdateListener(new ExtUmengUpdateListener());
		UmengUpdateAgent.setUpdateAutoPopup(false);
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.setDeltaUpdate(false);
		UmengUpdateAgent.setSlotId("64545");
		UmengUpdateAgent.forceUpdate(getContext());

		return true;
	}

	public boolean onClearCachePreferenceClick(Preference preference) {
		DataHelper.getSingleton().clearCache(getContext(), new ClearCacheTaskICallback());

		FileUtils.deleteQuietly(getContext().getCacheDir());
		FileUtils.deleteQuietly(getContext().getExternalCacheDir());

		return true;
	}

	public boolean onNightModePreferenceChange(Preference preference, Object value) {
		SettingSP.getSingleton(getContext()).setNightMode((Boolean) value);

		return true;
	}

	public boolean onAlwaysOnPreferenceChange(Preference preference, Object value) {
		SettingSP.getSingleton(getContext()).setAlwaysOn((Boolean) value);

		return true;
	}

	public boolean onTradionalPreferenceChange(Preference preference, Object value) {
		SettingSP.getSingleton(getContext()).setTraditional((Boolean) value);

		return true;
	}

	private class ExtUmengUpdateListener implements UmengUpdateListener {

		@Override
		public void onUpdateReturned(int status, UpdateResponse response) {
			switch (status) {
				case UpdateStatus.Yes:
					UmengUpdateAgent.showUpdateDialog(getContext(), response);
					break;
				case UpdateStatus.No:
					ToastUtil.show(getContext(), R.string.umeng_update_no);
					break;
				case UpdateStatus.Timeout:
					ToastUtil.show(getContext(), R.string.umeng_update_timeout);
					break;
			}
		}
	}

	private class ClearCacheTaskICallback implements ClearCacheTask.ICallback {

		@Override
		public void onStart() {
			clearCacheDialog.show();
		}

		@Override
		public void onFinish() {
			clearCacheDialog.hide();
		}
	}
}
